package com.hi.hiparty.manager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hi.hiparty.dao.PartyDao;
import com.hi.hiparty.dao.query.PartyDBQuery;
import com.hi.hiparty.entity.Party;
import com.hi.hiparty.solr.PartyQuery;
import com.hi.hiparty.solr.Result;
import com.hi.hiparty.solr.SolrUtils;

@Service
public class PartyManager {

	private static final Log logger = LogFactory.getLog(PartyManager.class);

	@Autowired
	private PartyDao partyDao;

	public PartyResult publishParty(Party party) throws Exception {
		PartyResult result = new PartyResult();
		try {
			partyDao.insert(party);
		} catch (Exception e) {
			logger.error("insert party error.", e);
			result.setSuccess(false);
			result.setErrorCode(PartyResult.ERROR_DB_EXCEPTION);
			result.setErrorMsg(e.getMessage());
		}
		try {
			if (0 != SolrUtils.addParty(party)) {
				result.setSuccess(false);
				result.setErrorCode(PartyResult.ERROR_SOLR);
			}
		} catch (Exception e) {
			logger.error("add party2solr error.", e);
			result.setSuccess(false);
			result.setErrorCode(PartyResult.ERROR_SOLR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public PartyResult updateParty(Party party) throws Exception {
		PartyResult result = new PartyResult();
		int id = -1;
		Party dbparty = null;
		try {
			id = partyDao.updateParty(party);
			dbparty = partyDao.getPartyById(party.getPartyid());
		} catch (Exception e) {
			logger.error("update party error.", e);
			result.setSuccess(false);
			result.setErrorCode(PartyResult.ERROR_DB_EXCEPTION);
			result.setErrorMsg(e.getMessage());
		}
		if (id > 0 && null != dbparty) {
			try {
				if (0 != SolrUtils.addParty(dbparty)) {
					result.setSuccess(false);
					result.setErrorCode(PartyResult.ERROR_SOLR);
				}
			} catch (Exception e) {
				logger.error("update party2solr error.", e);
				result.setSuccess(false);
				result.setErrorCode(PartyResult.ERROR_SOLR);
				result.setErrorMsg(e.getMessage());
			}
		} else {
			result.setSuccess(false);
			result.setErrorCode(PartyResult.ERROR_DB_NO_RESULT);
		}
		return result;
	}

	public PartyResult updateUserPos(int userid, double lon, double lat) throws Exception {
		PartyResult result = new PartyResult();
		List<Party> pList = null;
		try {
			pList = partyDao.getPartyByUserId(userid);
		} catch (Exception e) {
			logger.error("get partys error.", e);
			result.setSuccess(false);
			result.setErrorCode(PartyResult.ERROR_DB_EXCEPTION);
			result.setErrorMsg(e.getMessage());
		}
		if (null != pList && pList.size() > 0) {
			for (Party party : pList) {
				party.setUserPos(lon + "," + lat);
			}
			try {
				if (0 != SolrUtils.addPartys(pList)) {
					logger.error("add partys2solr error.");
					result.setSuccess(false);
					result.setErrorCode(PartyResult.ERROR_SOLR);
				}
			} catch (Exception e) {
				logger.error("add partys2solr error.", e);
				result.setSuccess(false);
				result.setErrorCode(PartyResult.ERROR_SOLR);
				result.setErrorMsg(e.getMessage());
			}
		} else {
			result.setSuccess(false);
			result.setErrorCode(PartyResult.ERROR_DB_NO_RESULT);
			// 是否有必要在更新db坐标用户坐标？
			partyDao.updateUserPosByUserId(userid, lon, lat);
		}
		return result;
	}

	public PartyResult queryPartys(PartyQuery pQuery) throws Exception {
		PartyResult result = new PartyResult();
		Result<Party> rs = SolrUtils.getPartyListByQueryPara(pQuery);
		if (rs.isSuccess() && !rs.getList().isEmpty()) {
			result.setPartys((rs.getList()));
			result.setCount(rs.getCount());
		} else {
			if (!rs.isSuccess()) {
				result.setSuccess(false);
				result.setErrorCode(PartyResult.ERROR_SOLR);
				result.setErrorMsg(rs.getErrorMsg());
			}
		}
		return result;
	}

	public PartyResult getUserPartys(String userid, int page, int pageSize) {
		PartyResult rs = new PartyResult();
		PartyDBQuery query = new PartyDBQuery();
		query.setUserid(userid);
		query.setPage(page);
		query.setPageSize(pageSize);
		try {
			List<Party> partys = partyDao.getPartysForQuery(query);
			int total = partyDao.getPartysCountForQuery(query);
			rs.setCount(total);
			rs.setPartys(partys);
		} catch (Exception e) {
			logger.error("get userPartys for db error.", e);
			rs.setSuccess(false);
			rs.setErrorMsg(e.getMessage());
		}
		return rs;
	}

	public List<Party> getPartysByTime(int start, int size) throws Exception {
		return partyDao.getPartysByTime(start, size);
	}
}
